package at.horario.web.pages;

import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;


/**
 * Login page constituents  are the same as Login.html except that ist is made up of equivalent Wicket components
 */
public class Login extends PageTemplate {
    private Form form;
    private String username;
    private String password;
    private String rememberMe;
    private static final String ADMIN = "admin";

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public Login() {

        TextField userIdField = new TextField("username", new PropertyModel(this, "username"));
        PasswordTextField passwordField = new PasswordTextField("password", new PropertyModel(this, "password"));
        CheckBox rememberMe = new CheckBox("rememberMe", new PropertyModel(this, "rememberMe"));


        passwordField.setResetPassword(false);

        form = new LoginForm("loginForm");
        form.setMarkupId("loginForm");
        form.add(userIdField);
        form.add(passwordField);
        form.add(rememberMe);

        add(form);

    }


    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(String rememberMe) {
        this.rememberMe = rememberMe;
    }

    public boolean signIn(String username, String password) {
        return AuthenticatedWebSession.get().signIn(username, password);
    }

    protected void onSignInFailed() {
        // Try the component based localizer first. If not found try the
        // application localizer. Else use the default
        String errMsg = getLocalizer().getString("errors.password.mismatch", Login.this);
        log.error(errMsg);
        error(errMsg);
    }

    protected void onSignInSucceeded() {
        // If login has been called because the user was not yet
        // logged in, than continue to the original destination,
        // otherwise to the Home page
        if (!continueToOriginalDestination()) {
            setResponsePage(getApplication().getSessionSettings().getPageFactory().newPage(
                    getApplication().getHomePage(), null));
        }
    }


    class LoginForm extends Form {
        public LoginForm(String id) {
            super(id);
        }

        @Override
        protected void onSubmit() {
            String userId = Login.this.getUsername();
            String password = Login.this.getPassword();

            if (signIn(userId, password)) onSignInSucceeded();
            else onSignInFailed();

        }
    }


}
